package ui;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class SqlInputDialog extends Dialog {

	protected Object result;
	protected Shell shell;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public SqlInputDialog(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(618, 387);
		shell.setText(getText());
		
		shell.setLocation(Display.getCurrent().getClientArea().width / 2 - shell.getShell().getSize().x/2, Display.getCurrent()
				.getClientArea().height / 2 - shell.getSize().y/2);
		
		final StyledText sqlInputText = new StyledText(shell, SWT.BORDER);
		sqlInputText.setBounds(89, 10, 513, 305);
		
		Button buttonOk = new Button(shell, SWT.NONE);
		buttonOk.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				result = sqlInputText.getText();
				shell.dispose();
			}
		});
		buttonOk.setBounds(432, 321, 80, 27);
		buttonOk.setText("\u786E\u5B9A");
		
		Button buttonCancel = new Button(shell, SWT.NONE);
		buttonCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				shell.dispose();
			}
		});
		buttonCancel.setBounds(522, 321, 80, 27);
		buttonCancel.setText("\u53D6\u6D88");
		
		Button maxLevelBtn = new Button(shell, SWT.NONE);
		maxLevelBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				result = "select number from stockanlyz WHERE maxToDays=0";
				shell.dispose();
			}
		});
		maxLevelBtn.setBounds(10, 10, 71, 25);
		maxLevelBtn.setText("\u9AD8\u4EF7\u9009\u5165");
		
		Button lowLevelBtn = new Button(shell, SWT.NONE);
		lowLevelBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				result = "select number from stockanlyz WHERE minToDays<5";
				shell.dispose();
			}
		});
		lowLevelBtn.setBounds(10, 41, 71, 25);
		lowLevelBtn.setText("\u4F4E\u4F4D\u9009\u5165");
		
		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				result = "select number from stockanlyz WHERE amtcredays<6 and amtcredays>=0";
				shell.dispose();
			}
		});
		btnNewButton.setBounds(10, 72, 73, 25);
		btnNewButton.setText("\u91CF\u589E\u9009\u5165");
		
		Button btnIsown = new Button(shell, SWT.NONE);
		btnIsown.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				result = "select number from stockanlyz WHERE isown=1";
				shell.dispose();
			}
		});
		btnIsown.setBounds(10, 103, 73, 25);
		btnIsown.setText("\u62E5\u6709");
	}
}
